package zz.servlet;

import zz.Dao.UserDao;
import zz.javabean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.*;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao userDao = new UserDao();
        User user = userDao.findUser(username);

        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(10);
        int count2 = (int) getServletContext().getAttribute("count2");
//        System.out.println(789789);
        if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
//            System.out.println(123123);
            count2++;
            getServletContext().setAttribute("count2", count2);
            session.setAttribute("success",username);
            //客户端跳转
            response.sendRedirect(request.getContextPath() + "/index.jsp");
////            response.sendRedirect("/main.jsp?userName=" + user.getUsername());

        }else {
//            System.out.println(45646);
            request.setAttribute("error","登录失败");
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }

//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//
//        final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//        final String DB_URL = "jdbc:mysql://localhost:3306/user?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//        final String USER = "root";
//        final String PASS = "123456";
//        Connection conn = null;
//        Statement stmt = null;
//        try{
//            // 注册 JDBC 驱动
//            Class.forName(JDBC_DRIVER);
//            // 打开链接
//            conn = DriverManager.getConnection(DB_URL,USER,PASS);
//            // 执行查询
//            stmt = conn.createStatement();
//            String sql;
//            sql = " SELECT * FROM tb_user where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"'";
//            ResultSet rs = stmt.executeQuery(sql);
//
//            if(rs.next()){    //查不到数据 sql执行false
//                String username2 = rs.getString("username");
//                String password2  = rs.getString("password");
//                System.out.println("查到了");
//                if(user.getUsername().equals(username2) && user.getPassword().equals(password2)){
//                    User usertemp = new User();
//                    usertemp.setUsername(username2);
//                    request.setAttribute("usertemp", usertemp);
//                    request.getRequestDispatcher("/success.jsp").forward(request, response);
//                }
//            }
//            else  {
//                request.setAttribute("errInfo", "用户名或密码不正确！");
//                System.out.println("没查到");
//                request.getRequestDispatcher("/login.jsp").forward(request, response);
//                //response.sendRedirect(request.getContextPath()+"/Login.jsp");
//            }
//            // 完成后关闭
//            rs.close();
//            stmt.close();
//            conn.close();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }finally {
//            // 关闭资源
//            try {
//                if (stmt != null) stmt.close();
//                if (conn != null) conn.close();
//            }
//            catch (Exception se) {
//                se.printStackTrace();
//            }
//        }
//
//
////        request.getRequestDispatcher("/success.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}
